/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

//import com.alipay.api.internal.util.AlipaySignature;
import com.zxy.adminlog.UsAdmin_Log;
//import com.zxy.business.Ope_DB_Go;
import com.zxy.business.Query_Data;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.EncryptUtil;
import com.zxy.common.MD5Util;
import com.zxy.db_Self.DBManager_Self;
import com.zxy.model.Return_Value;
import com.zxy.pay.AlipayConfig;
import com.zxy.pay.Alipay_now;
import com.zxy.pay.WxPayConfig;

public class ServerHandlerHttp extends Thread
{
	public static final String	WEB_ROOT	= Com_Para.ApplicationPath
			+ File.separator + "web";
	private Socket				mServSocket;
	private String				strIP		= "0.0.0.0";
	private Return_Value		rv			= new Return_Value();

	public ServerHandlerHttp(Socket clientSocket)
	{
		this.setName("ServerHandlerHttp_zxyong");
		if(Com_Fun.flagSconnections())
			this.mServSocket = clientSocket;
		else
			System.out.println("超过系统试用版本时间,请联系开发单位");
		try
		{
			if(DBManager_Self.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				init();
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}

	}

	// web内容加载
	private void webPage_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage, OutputStream output, String web_name_s) throws Exception
	{
		File file = new File(WEB_ROOT + Com_Para.zxyPath
				+ strUrl.toString().substring(0,strUrl.toString().indexOf(web_name_s))
				+ web_name_s);
		FileInputStream fis = null;
		InputStreamReader is = null;
		BufferedReader br = null;
		if(file.exists())
		{
			try
			{
				output.write(returnMessage.append("\r\n\r\n").toString().getBytes(Com_Para.U_CODE));
				int BUFFER_SIZE = 0;
				fis = new FileInputStream(file);
				byte[] sbytes = new byte[100];
				while((BUFFER_SIZE = fis.read(sbytes)) != -1)
				{
					output.write(sbytes,0,BUFFER_SIZE);
				}
			}
			catch(Exception e)
			{
			}
			finally
			{
				try
				{
					if(!java.util.Objects.equals(fis,null))
						fis.close();
					if(!java.util.Objects.equals(is,null))
						is.close();
					if(!java.util.Objects.equals(br,null))
						br.close();
				}
				catch(Exception e)
				{
				}
			}
			output.write("\r\n".getBytes());
			output.flush();
			output.close();
		}else
		{
			StringBuilder errorMessage = new StringBuilder("HTTP/1.1 404 File Not Found\r\n"
					+ "Content-Type: text/html\r\n" + "Content-Length: 230\r\n"
					+ "\r\n" + "<h1>未找到正确页面</h1>");
			try
			{
				output.write(errorMessage.toString().getBytes(Com_Para.U_CODE));
				output.write("\r\n".getBytes());
				output.flush();
				output.close();
			}
			catch(IOException e)
			{
			}
		}
	}

	// 发送错误信息
	private StringBuilder Send_Error(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		returnMessage.delete(0,returnMessage.length());
		returnMessage.append("Error请求语法错误");
		return returnMessage;
	}

	// 微信支付成功
	private StringBuilder WxPay_Rel(String strTPost, StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage, String strIP)
	{
		StringBuilder strAryResult = new StringBuilder();
		JSONObject jsob = null;
		try
		{
			if(strTPost.equals(""))
				return new StringBuilder("微信反馈信息为空：");
			else
				jsob = Com_Fun.xmlToJSON(strTPost).getJSONObject("xml");
			if(!java.util.Objects.equals(jsob,null)
					&& jsob.getString("result_code").toString().equals("SUCCESS"))
			{
				String param_value = "";
				ArrayList<String> aryParamValue = new ArrayList<String>();
				for(int i = 1;i < 20;i++)
				{
					switch(i)
					{
						case 1:
							param_value = jsob.getString("out_trade_no").toString();
							break;
						case 2:
							param_value = String.valueOf(Float.parseFloat(jsob.getString("total_fee").toString())
									/ 100);
							break;
						case 3:
							param_value = jsob.getString("time_end").toString();
							param_value = Com_Fun.DateTimeStrYYYYMMDDHHmmss(param_value,"yyyyMMddHHmmss");
							break;
						case 4:
							param_value = jsob.getString("transaction_id").toString();
							break;
						case 5:
							param_value = jsob.getString("openid").toString();
							break;
						case 6:
							param_value = jsob.getString("mch_id").toString();
							break;
						case 7:
							param_value = jsob.getString("appid").toString();
							break;
						case 8:
							param_value = jsob.getString("openid").toString();
							break;
						default:
							param_value = "";
					}
					aryParamValue.add(i - 1,param_value);
				}
				/* 签名验证 */
				WxPayConfig wxp = new WxPayConfig();
				StringBuilder stringSignTemp = new StringBuilder();
				stringSignTemp.append("appid=" + jsob.getString("appid")
						+ "&bank_type=" + jsob.getString("bank_type")
						+ "&cash_fee=" + jsob.getString("cash_fee")
						+ "&device_info=" + jsob.getString("device_info")
						+ "&fee_type=" + jsob.getString("fee_type")
						+ "&is_subscribe=" + jsob.getString("is_subscribe")
						+ "&mch_id=" + jsob.getString("mch_id") + "&nonce_str="
						+ jsob.getString("nonce_str") + "&openid="
						+ jsob.getString("openid") + "&out_trade_no="
						+ jsob.getString("out_trade_no") + "&result_code="
						+ jsob.getString("result_code") + "&return_code="
						+ jsob.getString("return_code") + "&time_end="
						+ jsob.getString("time_end") + "&total_fee="
						+ jsob.getString("total_fee") + "&trade_type="
						+ jsob.getString("trade_type") + "&transaction_id="
						+ jsob.getString("transaction_id") + "&key="
						+ wxp.srckey);
				String sign2 = MD5Util.MD5Encode(stringSignTemp.toString(),"UTF-8").toUpperCase();
				if(jsob.getString("sign").equals(sign2))
				{
					strAryResult = gd.GetDataList(Com_Para.sub_code,Com_Para.sub_usercode,"A01_Order_WxPay",aryParamValue,strUrl,"","",strIP,null);
					JSONObject json = null;
					try
					{
						json = new JSONObject(strAryResult.toString());
						if(!java.util.Objects.equals(json,null)
								&& !json.has("retcode"))
						{
							org.json.JSONArray scoreObj = (org.json.JSONArray) json.get("A01_Order_WxPay");
							strAryResult.delete(0,strAryResult.length());
							if(scoreObj.length() > 0
									&& ((JSONObject) scoreObj.get(0)).getString("s_result").equals("1"))
							{
								strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"1\",\"error_desc\":\"微信支付成功\"}]}");
							}else
							{
								strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"0\",\"error_desc\":\"微信支付失败,"
										+ ((JSONObject) scoreObj.get(0)).getString("error_desc")
										+ "\"}]}");
								this.rv.err_desc = strAryResult;
								this.rv.s_result = 0;
							}
						}
					}
					catch(Exception ess)
					{
						strAryResult.delete(0,strAryResult.length());
						strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"0\",\"error_desc\":\"微信支付失败,"
								+ json.getString("A01_Order_WxPay") + ";"
								+ ess.getMessage() + "\"}]}");
						this.rv.err_desc = strAryResult;
						this.rv.s_result = 0;
					}
				}else
				{
					strAryResult.delete(0,strAryResult.length());
					strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"0\",\"error_desc\":\"微信支付失败,微信签名校验失败\"}]}");
					this.rv.err_desc = strAryResult;
					this.rv.s_result = 0;
				}
			}else
			{
				strAryResult.delete(0,strAryResult.length());
				strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"0\",\"error_desc\":\"微信反馈信息错误："
						+ strTPost + "\"}]}");
				this.rv.err_desc = strAryResult;
				this.rv.s_result = 0;
			}
		}
		catch(JSONException e)
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Order_WxPay\":[{\"s_result\":\"0\",\"error_desc\":\"微信反馈信息错误："
					+ e.getMessage() + "\"}]}");
			this.rv.err_desc = strAryResult;
			this.rv.s_result = 0;
		}
		return strAryResult;
	}

	// 支付宝支付成功2
	private StringBuilder aliPay2_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		String[] strParameter = post_str;
		Map<String, String> params = new HashMap<String, String>();
		// System.out.println("*****支付宝异步通知开始*****");
		for(int i = 0;i < strParameter.length;i++)
		{
			String[] strTemPar = strParameter[i].split("=");
			if(strTemPar.length > 1)
			{
				if(strParameter[i].indexOf("sign=") == 0)
				{
					String strSign = strParameter[i].substring(5,strParameter[i].length());
					params.put("sign",strSign);
				}else
				{
					params.put(strTemPar[0],strTemPar[1]);
				}
			}
		}
		// *****支付宝异步通知验签*****";
		boolean signVerified = true;// AlipaySignature.rsaCheckV1(params,AlipayConfig.ALIPAY_PUBLIC_KEY,"UTF-8",AlipayConfig.SIGNTYPE);
		if((!java.util.Objects.equals(params.get("total_amount"),null))
				&& (!params.get("total_amount").toString().equals(""))
				&& (!java.util.Objects.equals(params.get("gmt_payment"),null))
				&& (!params.get("gmt_payment").toString().equals(""))
				&& (!java.util.Objects.equals(params.get("trade_no"),null))
				&& (!params.get("trade_no").toString().equals(""))
				&& (!java.util.Objects.equals(params.get("auth_app_id"),null))
				&& (!params.get("auth_app_id").toString().equals(""))
				&& (!java.util.Objects.equals(params.get("app_id"),null))
				&& (!params.get("app_id").toString().equals(""))
				&& (!java.util.Objects.equals(params.get("out_trade_no"),null))
				&& (!params.get("out_trade_no").toString().equals("")))
		{
			String param_value = "";
			ArrayList<String> aryParamValue = new ArrayList<String>();
			for(int i = 1;i < 20;i++)
			{
				switch(i)
				{
					case 1:
						param_value = java.util.Objects.equals(params.get("out_trade_no"),null) ? "" : (String) params.get("out_trade_no");
						break;
					case 2:
						param_value = java.util.Objects.equals(params.get("total_amount"),null) ? "" : (String) params.get("total_amount");
						break;
					case 3:
						param_value = java.util.Objects.equals(params.get("gmt_payment"),null) ? "" : (String) params.get("gmt_payment");
						break;
					case 4:
						param_value = java.util.Objects.equals(params.get("trade_no"),null) ? "" : (String) params.get("trade_no");
						break;
					case 5:
						param_value = java.util.Objects.equals(params.get("auth_app_id"),null) ? "" : (String) params.get("auth_app_id");
						break;
					case 6:
						param_value = java.util.Objects.equals(params.get("seller_id"),null) ? "" : (String) params.get("seller_id");
						break;
					case 7:
						param_value = java.util.Objects.equals(params.get("app_id"),null) ? "" : (String) params.get("app_id");
						break;
					case 8:
						param_value = java.util.Objects.equals(params.get("buyer_id"),null) ? "" : (String) params.get("buyer_id");
						break;
					default:
						param_value = "";
				}
				aryParamValue.add(i - 1,param_value);
			}

			if(signVerified)
			{
				strAryResult = gd.GetDataList(Com_Para.sub_code,Com_Para.sub_usercode,"A01_Order_AliPay",aryParamValue,strUrl,"","",strIP,null);
				JSONObject json = null;
				try
				{
					json = new JSONObject(strAryResult.toString());
					if(!java.util.Objects.equals(json,null)
							&& !json.has("retcode"))
					{
						org.json.JSONArray scoreObj = (org.json.JSONArray) json.get("A01_Order_AliPay");
						if(scoreObj.length() > 0
								&& ((JSONObject) scoreObj.get(0)).getString("s_result").equals("1"))
						{
							strAryResult.delete(0,strAryResult.length());
							strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"1\",\"error_desc\":\"支付宝支付成功\"}]}");
						}else
						{
							strAryResult.delete(0,strAryResult.length());
							strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败,"
									+ ((JSONObject) scoreObj.get(0)).getString("error_desc")
									+ "\"}]}");
							this.rv.err_desc = strAryResult;
							this.rv.s_result = 0;
						}
					}
				}
				catch(Exception ess)
				{
					strAryResult.delete(0,strAryResult.length());
					strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败,"
							+ json.getString("A01_Order_AliPay") + ";"
							+ ess.getMessage() + "\"}]}");
					this.rv.err_desc = strAryResult;
					this.rv.s_result = 0;
				}
			}else
			{
				strAryResult.delete(0,strAryResult.length());
				strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付失败,支付宝签名校验失败\"}]}");
				this.rv.err_desc = strAryResult;
				this.rv.s_result = 0;
			}
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝反馈信息错误："
					+ strUrl + "\"}]}");
			this.rv.err_desc = strAryResult;
			this.rv.s_result = 0;
		}
		return strAryResult;
	}

	// 支付宝支付成功3
	private StringBuilder aliPay3_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		Map<String, String> params = new HashMap<String, String>();
		JSONObject json = null;
		try
		{
			json = new JSONObject(post_str[0]);
			JSONObject trade_app = json.getJSONObject("alipay_trade_app_pay_response");
			@SuppressWarnings("unchecked")
			Iterator<String> sIterator = trade_app.keys();
			while(sIterator.hasNext())
			{
				String key = sIterator.next();
				String value = trade_app.getString(key);
				params.put(key,value);
			}
			params.put("sign",json.getString("sign").toString().replace(" ","+"));
		}
		catch(Exception e)
		{
		}
		if(!(java.util.Objects.equals(params.get("code"),null))
				&& !(params.get("code").toString().equals("10000"))
				&& !(params.get("total_amount").toString().equals(""))
				&& !(java.util.Objects.equals(params.get("timestamp"),null))
				&& !(params.get("timestamp").toString().equals(""))
				&& !(java.util.Objects.equals(params.get("trade_no"),null))
				&& !(params.get("trade_no").toString().equals(""))
				&& !(java.util.Objects.equals(params.get("auth_app_id"),null))
				&& !(params.get("auth_app_id").toString().equals(""))
				&& !(java.util.Objects.equals(params.get("app_id"),null))
				&& !(params.get("app_id").toString().equals(""))
				&& !(java.util.Objects.equals(params.get("out_trade_no"),null))
				&& !(params.get("out_trade_no").toString().equals("")))
		{
			String param_value = "";
			ArrayList<String> aryParamValue = new ArrayList<String>();
			for(int i = 1;i < 20;i++)
			{
				switch(i)
				{
					case 1:
						param_value = java.util.Objects.equals(params.get("out_trade_no"),null) ? "" : (String) params.get("out_trade_no");
						break;
					case 2:
						param_value = java.util.Objects.equals(params.get("total_amount"),null) ? "" : (String) params.get("total_amount");
						break;
					case 3:
						param_value = java.util.Objects.equals(params.get("timestamp"),null) ? "" : Com_Fun.s_decode((String) params.get("timestamp"),Com_Para.U_CODE);
						break;
					case 4:
						param_value = java.util.Objects.equals(params.get("trade_no"),null) ? "" : (String) params.get("trade_no");
						break;
					case 5:
						param_value = java.util.Objects.equals(params.get("auth_app_id"),null) ? "" : (String) params.get("auth_app_id");
						break;
					case 6:
						param_value = java.util.Objects.equals(params.get("seller_id"),null) ? "" : (String) params.get("seller_id");
						break;
					case 7:
						param_value = java.util.Objects.equals(params.get("app_id"),null) ? "" : (String) params.get("app_id");
						break;
					case 8:
						param_value = "-1"; // htParam.get("buyer_id"),null) ?
											// "" : (String)
											// htParam.get("buyer_id");
						break;
					default:
						param_value = "";
				}
				aryParamValue.add(i - 1,param_value);
			}
			// "*****支付宝同步通知*****");
			@SuppressWarnings("unused")
			boolean signVerified = false;// AlipaySignature.rsaCheckV1(params,AlipayConfig.ALIPAY_PUBLIC_KEY,"UTF-8",AlipayConfig.SIGNTYPE);
			if(true && params.get("msg").toString().equals("Success"))
			{
				strAryResult = gd.GetDataList(Com_Para.sub_code,Com_Para.sub_usercode,"A01_Order_AliPay",aryParamValue,strUrl,"","",strIP,null);
				try
				{
					json = new JSONObject(strAryResult.toString());
					if(!java.util.Objects.equals(json,null)
							&& !json.has("retcode"))
					{
						org.json.JSONArray scoreObj = (org.json.JSONArray) json.get("A01_Order_AliPay");
						if(scoreObj.length() > 0
								&& ((JSONObject) scoreObj.get(0)).getString("s_result").equals("1"))
						{
							strAryResult.delete(0,strAryResult.length());
							strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"1\",\"error_desc\":\"支付宝支付成功\"}]}");
						}else
						{
							strAryResult.delete(0,strAryResult.length());
							strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败:"
									+ ((JSONObject) scoreObj.get(0)).getString("error_desc")
									+ "\"}]}");
						}
					}
				}
				catch(Exception ess)
				{
					strAryResult.delete(0,strAryResult.length());
					strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败:"
							+ json.getString("A01_Order_AliPay") + ";"
							+ ess.getMessage() + "\"}]}");
				}
			}else
			{
				strAryResult.delete(0,strAryResult.length());
				strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败:验签失败\"}]}");
			}
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝支付失败:"
					+ strUrl + "\"}]}");
		}
		return strAryResult;
	}

	// 支付宝支付成功1
	private StringBuilder aliPay_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		String strGUIDS = Com_Fun.Get_New_GUID();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		String strPaySuccess = "";
		for(String strTemV: strUrl.toString().split("&"))
		{
			strPaySuccess += strTemV + "\r\n";
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length > 1)
			{
				htParam.put(strTemPar[0],strTemPar[1]);
			}
		}

		if((!java.util.Objects.equals(htParam.get("total_amount"),null))
				&& (!htParam.get("total_amount").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("gmt_payment"),null))
				&& (!htParam.get("gmt_payment").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("trade_no"),null))
				&& (!htParam.get("trade_no").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("auth_app_id"),null))
				&& (!htParam.get("auth_app_id").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("app_id"),null))
				&& (!htParam.get("app_id").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("out_trade_no"),null))
				&& (!htParam.get("out_trade_no").toString().equals(""))
				&& (!java.util.Objects.equals(htParam.get("buyer_id"),null))
				&& (!htParam.get("buyer_id").toString().equals("")))
		{
			String param_name = "A01_Order_AliPay";
			String sub_code = Com_Para.sub_code;
			String sub_usercode = Com_Para.sub_usercode;
			String param_value = "";
			ArrayList<String> aryParamValue = new ArrayList<String>();
			for(int i = 1;i < 20;i++)
			{
				switch(i)
				{
					case 1:
						param_value = java.util.Objects.equals(htParam.get("out_trade_no"),null) ? "" : (String) htParam.get("out_trade_no");
						break;
					case 2:
						param_value = java.util.Objects.equals(htParam.get("total_amount"),null) ? "" : (String) htParam.get("total_amount");
						break;
					case 3:
						param_value = java.util.Objects.equals(htParam.get("gmt_payment"),null) ? "" : Com_Fun.s_decode((String) htParam.get("gmt_payment"),Com_Para.U_CODE);
						break;
					case 4:
						param_value = java.util.Objects.equals(htParam.get("trade_no"),null) ? "" : (String) htParam.get("trade_no");
						break;
					case 5:
						param_value = java.util.Objects.equals(htParam.get("auth_app_id"),null) ? "" : (String) htParam.get("auth_app_id");
						break;
					case 6:
						param_value = java.util.Objects.equals(htParam.get("seller_id"),null) ? "" : (String) htParam.get("seller_id");
						break;
					case 7:
						param_value = java.util.Objects.equals(htParam.get("app_id"),null) ? "" : (String) htParam.get("app_id");
						break;
					case 8:
						param_value = java.util.Objects.equals(htParam.get("buyer_id"),null) ? "" : (String) htParam.get("buyer_id");
						break;
					default:
						param_value = "";
				}
				aryParamValue.add(i - 1,param_value);
			}
			String delay_data = "";
			String delay_code = "";
			if((!java.util.Objects.equals(htParam.get("trade_status"),null))
					&& htParam.get("trade_status").toString().equals("TRADE_SUCCESS"))
			{
				strAryResult = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,null);
			}else
			{
				strAryResult.delete(0,strAryResult.length());
				strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\""
						+ strGUIDS + "支付失败："
						+ htParam.get("trade_status").toString() + "\"}]}");
			}
		}else if((!java.util.Objects.equals(htParam.get("trade_status"),null))
				&& htParam.get("trade_status").toString().equals("TRADE_SUCCESS"))
		{

			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"1\",\"error_desc\":\""
					+ strGUIDS + "支付成功：" + strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder(strGUIDS
					+ "支付成功数据[" + strPaySuccess + "]"));
			uL.WriteLog();
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Order_AliPay\":[{\"s_result\":\"0\",\"error_desc\":\"支付失败数据："
					+ strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder(strGUIDS
					+ "支付失败数据[" + strPaySuccess + "]"));
			uL.WriteLog();
		}
		return strAryResult;
	}

	// 微信授权2
	@SuppressWarnings("unused")
	private StringBuilder wxToken2_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		String strPaySuccess = "";
		for(String strTemV: strUrl.toString().split("&"))
		{
			strPaySuccess += strTemV + "\r\n";
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length > 1)
			{
				htParam.put(strTemPar[0],strTemPar[1]);
			}
		}
		if((!java.util.Objects.equals(htParam.get("param_value1"),null))
				&& (!htParam.get("param_value1").equals("")))
		{
			WxPayConfig an = new WxPayConfig();
			strAryResult = an.Wx_Login2(htParam.get("param_value1").toString(),htParam.get("param_value2").toString(),htParam.get("param_value3").toString());// an.aliToken(htParam.get("param_value1").toString());

		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Wx_Login_OK\":[{\"s_result\":\"0\",\"error_desc\":\"微信获取令牌失败数据;"
					+ strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("微信授权失败数据["
					+ strPaySuccess + "]"));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e)
			{
			}
		}
		return strAryResult;
	}

	// 微信授权1
	private StringBuilder wxToken_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		String strPaySuccess = "";
		for(String strTemV: strUrl.toString().split("&"))
		{
			strPaySuccess = strPaySuccess + strTemV + "\r\n";
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length > 1)
			{
				htParam.put(strTemPar[0],strTemPar[1]);
			}
		}
		if((!java.util.Objects.equals(htParam.get("param_value1"),null))
				&& (!htParam.get("param_value1").equals("")))
		{
			WxPayConfig an = new WxPayConfig();
			strAryResult = an.Wx_Login(htParam.get("param_value1").toString());// an.aliToken(htParam.get("param_value1").toString());
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Wx_Login_OK\":[{\"s_result\":\"0\",\"error_desc\":\"微信获取令牌失败数据;"
					+ strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("微信授权失败数据["
					+ strPaySuccess + "]"));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e)
			{
			}
		}
		return strAryResult;
	}

	// 支付宝获取令牌
	private StringBuilder aliToken_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		String strPaySuccess = "";
		for(String strTemV: strUrl.toString().split("&"))
		{
			strPaySuccess = strPaySuccess + strTemV + "\r\n";
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length > 1)
			{
				htParam.put(strTemPar[0],strTemPar[1]);
			}
		}
		if((!java.util.Objects.equals(htParam.get("param_value1"),null))
				&& (!htParam.get("param_value1").equals("")))
		{
			Alipay_now an = new Alipay_now();
			strAryResult = an.Ali_Login(htParam.get("param_value1").toString());// an.aliToken(htParam.get("param_value1").toString());
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Ali_Login_OK\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝获取令牌失败数据;"
					+ strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("支付宝授权失败数据["
					+ strPaySuccess + "]"));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e)
			{
			}
		}
		return strAryResult;
	}

	// 支付宝登录授权
	private StringBuilder aliAuth_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage) throws Exception
	{
		StringBuilder strAryResult = new StringBuilder();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		String strPaySuccess = "";
		for(String strTemV: strUrl.toString().split("&"))
		{
			strPaySuccess = strPaySuccess + strTemV + "\r\n";
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length > 1)
			{
				htParam.put(strTemPar[0],strTemPar[1]);
			}
		}
		if((!java.util.Objects.equals(htParam.get("auth_code"),null))
				&& (!htParam.get("auth_code").equals("")))
		{
			String param_name = "A01_Ali_Login";
			String sub_code = Com_Para.sub_code;
			String sub_usercode = Com_Para.sub_usercode;
			String param_value = "";
			ArrayList<String> aryParamValue = new ArrayList<String>();
			for(int i = 1;i < 20;i++)
			{
				switch(i)
				{
					case 1:
						param_value = java.util.Objects.equals(htParam.get("auth_code"),null) ? "" : (String) htParam.get("auth_code");
						break;
					default:
						param_value = "";
				}

				aryParamValue.add(i - 1,param_value);
			}
			String delay_data = "";
			String delay_code = "";
			strAryResult = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,htParam);
		}else
		{
			strAryResult.delete(0,strAryResult.length());
			strAryResult.append("{\"A01_Ali_Login\":[{\"s_result\":\"0\",\"error_desc\":\"支付宝授权失败数据;"
					+ strPaySuccess + "\"}]}");
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("支付宝授权失败数据["
					+ strPaySuccess + "]"));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e)
			{
			}
		}
		return strAryResult;
	}

	// 通用接口信息
	private StringBuilder Center_Data_Rel(StringBuilder strUrl, String s_guid, String[] post_str, Query_Data gd, StringBuilder returnMessage, String strIP) throws Exception
	{
		StringBuilder strAry = new StringBuilder();
		Hashtable<String, String> htParam = new Hashtable<String, String>();
		for(String strTemV: strUrl.toString().split("&"))
		{
			String[] strTemPar = strTemV.split("=");
			if(strTemPar.length == 2)
				htParam.put(strTemPar[0],strTemPar[1]);
			else
				htParam.put(strTemPar[0],"");
		}
		String sub_code = java.util.Objects.equals(htParam.get("sub_code"),null) ? "" : (String) htParam.get("sub_code");
		String sub_usercode = java.util.Objects.equals(htParam.get("sub_usercode"),null) ? "" : (String) htParam.get("sub_usercode");
		String delay_data = java.util.Objects.equals(htParam.get("delay_data"),null) ? "" : (String) htParam.get("delay_data");
		String delay_code = java.util.Objects.equals(htParam.get("delay_code"),null) ? "" : (String) htParam.get("delay_code");
		String param_name = java.util.Objects.equals(htParam.get("param_name"),null) ? "" : (String) htParam.get("param_name");
		String session_id = java.util.Objects.equals(htParam.get("session_id"),null) ? "" : (String) htParam.get("session_id");
		String login_id = java.util.Objects.equals(htParam.get("login_id"),null) ? "" : (String)htParam.get("login_id");
		String param_dbname = java.util.Objects.equals(htParam.get("param_dbname"),null) ? "" : (String) htParam.get("param_dbname");
		String jsonpzxyong = java.util.Objects.equals(htParam.get("jsoncallback"),null) ? "" : (String) htParam.get("jsoncallback");
		String is_xml = java.util.Objects.equals(htParam.get("is_xml"),null) ? "" : (String) htParam.get("is_xml");
		// s_guid = htParam.get("s_guid"),null) ? "" : (String)
		// htParam.get("s_guid");
		// String s_flag = htParam.get("s_flag"),null) ? "0" :
		// (String)htParam.get("s_flag");
		if(!param_dbname.equals(""))
			gd.param_dbname = param_dbname + ".";
		this.rv.param_name = param_name;
		// 是否需要登录flag
		boolean bFlag = false;
		if(!(Com_Para.Is_UserLogin.equals(""))
				&& (Com_Para.Is_UserLogin.indexOf("[" + param_name
						+ "]") != -1))
		{
			bFlag = true;
		}
		// 传递in 参数
		ArrayList<String> aryParamValue = new ArrayList<String>();
		for(int i = 1;i < 101;i++)
		{
			boolean tembFlag = false;
			String strVS = "";
			if(!java.util.Objects.equals(htParam.get("param_value"
					+ String.valueOf(i)),null))
			{
				strVS = (String) htParam.get("param_value" + String.valueOf(i));
				if(i == 1 || i == 2)
					strVS = Com_Fun.getParam_value1(strVS.trim());
				tembFlag = true;
			}else
			{
				for(String temstr: post_str)
				{
					if(temstr.indexOf("param_value" + String.valueOf(i)
							+ "=") == 0)
					{
						tembFlag = true;
					}else if(temstr.equals("jsonpzxyong"))
						jsonpzxyong = temstr.split("=")[1];
					if((temstr.indexOf("param_value" + String.valueOf(i)
							+ "=") == 0) && (temstr.split("=").length > 1))
					{
						strVS = temstr.split("=")[1];
						if(i == 1 || i == 2)
							strVS = Com_Fun.getParam_value1(strVS);
						break;
					}
				}
			}

			if(!tembFlag)
				break;
			if((bFlag) && (i == 2))
			{
				if(Com_Para.EncryptUtilType == 1)
					strVS = EncryptUtil.getSM32(strVS);
				else if(Com_Para.EncryptUtilType == 2)
					strVS = EncryptUtil.getMd5(strVS).toLowerCase();
				else if(Com_Para.EncryptUtilType == 3)
					strVS = com.zxy.common.CustomSHA256PasswordValidator.encode(strVS);
			}
			aryParamValue.add(i - 1,strVS);
		}
		// 用户登录接口
		if(bFlag)
		{
			gd.session_id = Com_Fun.Get_New_GUID().replace("-","");
			strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,htParam);
		}
		// 非登录接口但需要session_id验证
		else if((Com_Para.Login_Session == 1)
				&& (Com_Fun.GetInterSession(param_name)))
		{
			gd.session_id = session_id;
//			if(Com_Fun.GetInterSession(param_name))
//			{
			if((session_id.equals(""))
					|| (!Com_Fun.HadSession(session_id,login_id)))
			{
				strAry.delete(0,strAry.length());
				strAry.append("{\"" + param_name + "\":[{\""
						+ Com_Fun.getLowUpp("session_id") + "\":\"\",\""
						+ Com_Fun.getLowUpp("s_result") + "\":\"0\",\""
						+ Com_Fun.getLowUpp("error_desc")
						+ "\":\"用户未登陆,请登陆\"}]}");
			}else
			{
				strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,htParam);
			}
//			}else
//			{
//				strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,htParam);
//			}
		}
		// 通用接口
		else
		{
			strAry = gd.GetDataList(sub_code,sub_usercode,param_name,aryParamValue,strUrl,delay_data,delay_code,strIP,htParam);
		}

		if(!jsonpzxyong.equals(""))
		{
			strAry.insert(0,jsonpzxyong + "(").append(")");
		}
		if(this.rv.s_result == 1)
			this.rv = gd.rv;
		if(is_xml.equals("1"))
		{
			String temMsg = strAry.toString();
			JSONObject jso = null;
			jso = new JSONObject(temMsg);
			temMsg = Com_Fun.JSONToXml(jso.get(param_name).toString(),param_name);
			strAry.delete(0,strAry.length());
			strAry.append(temMsg);
		}
		// System.gc();
		htParam.clear();
		htParam = null;
		return strAry;
	}

	// 发送服务端接收请求
	private void Client_Send(StringBuilder strUrl, Socket clientSocket) throws IOException
	{
		if(strUrl.indexOf("@##ST=50") != -1)
		{
			// String DEVICE_IP =
			// clientSocket.getInetAddress().getHostAddress();
			// String DEVICE_PORT = String.valueOf(clientSocket.getPort());
			// Ope_DB_Go odg = new Ope_DB_Go();
			// odg.Set_Reg_Device(strUrl,DEVICE_IP,DEVICE_PORT);
		}
	}

	// 线程init
	private void init()
	{
		/* 跟踪当前socket连接数 */
		Com_Para.socket_count++;
		// System.out.println("当前增加线程,总数:"+String.valueOf(Com_Para.socket_count));
		InputStream input = null;
		OutputStream output = null;
		StringBuilder strGS = new StringBuilder("HTTP/1.1 200 OK\r\n"
				+ "Content-Type: text/html\r\n"
				+ "Access-Control-Allow-Methods: POST,GET\r\n"
				+ "Access-Control-Allow-Origin: *\r\n"// +
														// Com_Para.HttpUrl
				+ "Connection: Keep-Alive");// Keep-Alive
		try
		{
			String s_guid = "";
			strIP = this.mServSocket.getRemoteSocketAddress().toString();
			strIP = strIP.substring(1,strIP.indexOf(":"));
			input = this.mServSocket.getInputStream();
			output = this.mServSocket.getOutputStream();
			Request request = new Request(input);
			request.strIP = strIP;
			request.parse();
			Response response = new Response(output);
			response.setRequest(request);
			StringBuilder strUrl = request.getUri();
			String[] post_str = request.getPost_str();
			/* 判断request是否出现异常 */
			this.rv = request.rv;
			StringBuilder returnMessage = new StringBuilder("HTTP/1.1 200 OK\r\n");
			if(strUrl.indexOf(".html") != -1)
				returnMessage.append("Content-Type: text/html\r\n");
			else if(strUrl.indexOf(".js") != -1)
				returnMessage.append("Content-Type: application/x-javascript\r\n");
			else if(strUrl.indexOf(".css") != -1)
				returnMessage.append("Content-Type: text/css\r\n");
			else if(strUrl.indexOf(".jpg") != -1)
				returnMessage.append("Content-Type: image/jpg\r\n");
			else if(strUrl.indexOf(".gif") != -1)
				returnMessage.append("Content-Type: image/jpg\r\n");
			else if(strUrl.indexOf(".png") != -1)
				returnMessage.append("Content-Type: mage/png\r\n");
			else if(strUrl.indexOf(".svg") != -1)
				returnMessage.append("Content-Type: text/svg+xml\r\n");
			else if(strUrl.indexOf(".eot") != -1)
				returnMessage.append("Content-Type: application/vnd.ms-fontobject\r\n");
			else if(strUrl.indexOf(".ttf") != -1)
				returnMessage.append("Content-Type: application/x-font-ttf\r\n");
			else if(strUrl.indexOf(".woff") != -1)
				returnMessage.append("Content-Type: application/x-font-woff\r\n");
			else if(strUrl.indexOf(".woff2") != -1)
				returnMessage.append("Content-Type: application/x-font-woff\r\n");
			else if(strUrl.indexOf(".ico") != -1)
				returnMessage.append("Content-Type: image/ico\r\n");
			else
				returnMessage.append("Content-Type: text/html\r\n");
			returnMessage.append("Access-Control-Allow-Methods: POST,GET\r\n"
					+ "Access-Control-Allow-Origin:*" + Com_Para.HttpUrl
					+ "\r\n" + "Connection: Keep-Alive");
			StringBuilder strResult = new StringBuilder("-1");
			Query_Data gd = new Query_Data(this.rv);
			// System.out.println("当前url:"+strUrl);
			// 通用接口
			String temWeb_Name = Com_Fun.Get_Web_Name(strUrl);
			if(!temWeb_Name.equals(""))
			{
				webPage_Rel(strUrl,s_guid,post_str,gd,returnMessage,output,temWeb_Name);
			}
			// 通用数据接口
			else if((strUrl.indexOf("sub_code=") != -1)
					&& (strUrl.indexOf("param_name=") != -1))
			{
				strResult.delete(0,strResult.length());
				strResult = Center_Data_Rel(strUrl,s_guid,post_str,gd,returnMessage,strIP);
			}
			// 微信支付成功
			else if(strUrl.indexOf("WxPay.html") != -1)
			{
				String strTPost = "";
				for(String strTem: post_str)
				{
					strTPost += "\r\n" + strTem;
				}
				strResult.delete(0,strResult.length());
				strResult = WxPay_Rel(strTPost,strUrl,s_guid,post_str,gd,returnMessage,strIP);
			}
			// 支付宝登录授权
			else if(strUrl.indexOf("aliAuth=tue") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = aliAuth_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 支付宝获取令牌
			else if(strUrl.indexOf("aliToken=true") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = aliToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 微信获取令牌
			else if(strUrl.indexOf("wxToken=true") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = wxToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 微信获取令牌
			else if(strUrl.indexOf("wxToken2=true") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = wxToken_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 支付宝支付成功1
			else if(strUrl.indexOf("aliPay=tue") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = aliPay_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 支付宝支付成功2
			else if(strUrl.indexOf("AliPay.html") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = aliPay2_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			// 支付宝支付成功3
			else if(strUrl.indexOf("aliPay3=true") != -1)
			{
				strResult.delete(0,strResult.length());
				strResult = aliPay3_Rel(strUrl,s_guid,post_str,gd,returnMessage);
			}
			//
			else if(strUrl.indexOf("shared_session_id?") != -1
					&& (strIP.equals("0") || strIP.equals("127.0.0.1")))
			{
				String insession_id = strUrl.toString().split(" ")[1];
				insession_id = insession_id.substring(insession_id.indexOf("?")
						+ 1,insession_id.length());
				strResult.delete(0,strResult.length());
				strResult.append("{\"ret\"=\""
						+ String.valueOf(Com_Fun.HadSession(insession_id,""))
						+ "\",\"session_id\"=\"" + insession_id + "\"}");
			}
			// 其他信息
			else if((!strUrl.toString().trim().equals(""))
					&& (strUrl.toString().trim().indexOf("GET /favicon.ico HTTP/1.1") != -1))
			{
				strResult.delete(0,strResult.length());
				strResult = Send_Error(strUrl,s_guid,post_str,gd,returnMessage);
			}

			if(!strResult.toString().equals("-1"))
			{
				// System.out.println("返回值:"
				// + (returnMessage.toString() + "\r\n\r\n"
				// + strResult.toString() + "\r\n"));
				output.write((returnMessage.toString() + "\r\n\r\n"
						+ strResult.toString()
						+ "\r\n").getBytes(Com_Para.U_CODE));
				output.flush();
				output.close();
			}
			// 发送统一服务内容
			Client_Send(strUrl,this.mServSocket);
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
			try
			{
				output.write(strGS.append("\r\n\r\n").append(e.getMessage()).append("\r\n").toString().getBytes(Com_Para.U_CODE));
			}
			catch(IOException e1)
			{
			}
		}
		finally
		{
			try
			{
				if(!java.util.Objects.equals(input,null))
					input.close();
				if(!java.util.Objects.equals(output,null))
					output.close();

				if(!java.util.Objects.equals(this.mServSocket,null))
				{
					// try
					// {
					// if(!this.mServSocket.isClosed())
					// {
					// // clientSocket.setSoLinger(true, 0);
					// this.mServSocket.shutdownInput();
					// this.mServSocket.shutdownOutput();
					// clientSocket.close();
					// /* 统计socket连接 */
					// }
					// }
					// catch(Exception ee)
					// {
					// System.out.println("错误" + ee.getMessage());
					// }
					Com_Para.socket_count--;
					// System.out.println("当前减少线程,总数:"+String.valueOf(Com_Para.socket_count));
				}
			}
			catch(Exception ees)
			{
			}
		}
		// System.gc();
	}

	public void run()
	{
		try
		{
			if(!DBManager_Self.connectionPool.jdbcDriver.equals("org.sqlite.JDBC"))
				init();
		}
		catch(Exception e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
	}
}